package com.hsc.orderservice.service.impl;

import com.hsc.commonutils.ordervo.CourseWebVoOrder;
import com.hsc.commonutils.ordervo.UcenterMemberOrder;
import com.hsc.orderservice.client.EduClient;
import com.hsc.orderservice.client.UcenterClient;
import com.hsc.orderservice.entity.Order;
import com.hsc.orderservice.mapper.OrderMapper;
import com.hsc.orderservice.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsc.orderservice.util.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-19
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //生成订单的方法
    @Override
    public String createOrders(String courseId, String id) {
        //通过远程调用获取用户信息
        UcenterMemberOrder ucenterMember = ucenterClient.getUserInfoOrder(id);
        //通过远程调用根据课程id获取课程信息
        CourseWebVoOrder courseDto = eduClient.getCourseInfoOrder(courseId);
        //创建order对象，重新赋值
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo()); //调用工具类生成订单号
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle()); //课程名称
        order.setCourseCover(courseDto.getCover()); //课程封面
        order.setTeacherName("test"); //讲师名称，course对象中只有讲师id，没有讲师名称
        order.setTotalFee(courseDto.getPrice()); //课程金额
        order.setMemberId(id); //用户id
        order.setMobile(ucenterMember.getMobile()); //用户手机
        order.setNickname(ucenterMember.getNickname()); //用户昵称
        order.setStatus(0);//0代表未支付
        order.setPayType(1);//支付类型
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }
}
